package cn.sdormitory.school.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.constant.CommonConstant;
import cn.sdormitory.common.utils.DateTimeUtils;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.school.dao.SchoolDutyDao;
import cn.sdormitory.school.entity.SchoolDuty;
import cn.sdormitory.school.service.SchoolDutyService;
import cn.sdormitory.smartclass.entity.ClassRepair;
import cn.sdormitory.smartclass.vo.ClassDvVo;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.vo.LoginInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("schoolDutyService")
public class SchoolDutyServiceImpl extends ServiceImpl<SchoolDutyDao, SchoolDuty> implements SchoolDutyService {

    @Autowired
    private SchoolDutyDao schoolDutyDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public IPage<SchoolDuty> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        String dutyDate = (String) params.get("dutyDate");
        String month = (String) params.get("month");
        String dutyType = (String) params.get("dutyType");
        Boolean isToday =  (Boolean) params.get("today");
        String nowDate = DateUtil.today();
        LambdaQueryWrapper<SchoolDuty> wrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotEmpty(dutyDate)) {
            wrapper.eq(SchoolDuty::getDutyDate, dutyDate);
        }
        if (StrUtil.isNotEmpty(month)) {
            wrapper.likeRight(SchoolDuty::getDutyDate, month.substring(0,month.length()-3));
        }
        if(StrUtil.isNotEmpty(dutyType)){
            wrapper.eq(SchoolDuty::getDutyType, dutyType);
        }
        if(!StringUtils.isEmpty(isToday)){
            if(isToday){
                wrapper.eq(SchoolDuty::getDutyDate,nowDate).or().ge(SchoolDuty::getDutyDate,nowDate);
            }

        }
        wrapper.orderBy(true,true,SchoolDuty::getDutyDate);

        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public int createOrUpdateSchoolDuty(SchoolDuty schoolDuty) {
//        LambdaQueryWrapper<SchoolDuty> wrapper = new LambdaQueryWrapper<>();
//        if(!StringUtils.isEmpty(schoolDuty.getId())){
//            wrapper.ne(SchoolDuty::getId,schoolDuty.getId());
//        }
//        wrapper.eq(SchoolDuty::getUserId,schoolDuty.getUserId());
//        wrapper.eq(SchoolDuty::getDutyDate,schoolDuty.getDutyDate());
//        wrapper.eq(SchoolDuty::getDutyDept,schoolDuty.getDutyDept());
//        wrapper.eq(SchoolDuty::getDutyType,schoolDuty.getDutyType());
//        int count = schoolDutyDao.selectCount(wrapper);
//        //值班时间有重复
//        if(count>0) return -1;
        int num = 0;
        if(StringUtils.isEmpty(schoolDuty.getId())){
            schoolDuty.setCreatedTime(new Date());
            schoolDuty.setCreateBy(SysUserUtils.getSysUser().getUsername());
            num = schoolDutyDao.insert(schoolDuty);
        }else{
            schoolDuty.setModifyTime(new Date());
            schoolDuty.setModifyBy(SysUserUtils.getSysUser().getUsername());
            num = schoolDutyDao.updateById(schoolDuty);
        }
        return num;
    }

    @Override
    public int deleteSchoolDuty(Long id) {
        return schoolDutyDao.deleteById(id);
    }

    @Override
    public int importDutyList(SchoolDuty schoolDuty) {
        //查询userId
        LoginInfoVo userVO = sysUserDao.getLoginInfo(schoolDuty.getUserName());
        if(userVO==null){
            //老师不存在
            return -10;
        }
        schoolDuty.setUserId(userVO.getLoginId());
        if ("启蒙星".equals(schoolDuty.getDutyDeptName())){
            schoolDuty.setDutyDept("1");
        } else if ("BCSP".equals(schoolDuty.getDutyDeptName())){
            schoolDuty.setDutyDept("2");
        }else{
            return -12;
        }
        if ("部门经理值班".equals(schoolDuty.getDutyTypeName())){
            schoolDuty.setDutyType("1");
        }else if ("部门成员值班".equals(schoolDuty.getDutyTypeName())){
            schoolDuty.setDutyType("2");
        }else {
            return -13;
        }
        //查询相同部门人员值班是否重复
//        LambdaQueryWrapper<SchoolDuty> wrapper = new LambdaQueryWrapper<>();
//        if(!StringUtils.isEmpty(schoolDuty.getId())){
//            wrapper.ne(SchoolDuty::getId,schoolDuty.getId());
//        }
//        wrapper.eq(SchoolDuty::getUserId,schoolDuty.getUserId());
//        wrapper.eq(SchoolDuty::getDutyDate,schoolDuty.getDutyDate());
//        wrapper.eq(SchoolDuty::getDutyType,schoolDuty.getDutyType());
//        int count = schoolDutyDao.selectCount(wrapper);
//        //值班时间重复
//        if(count>0) return -11;
        schoolDuty.setCreatedTime(new Date());
        schoolDuty.setCreateBy(SysUserUtils.getSysUser().getUsername());
        int num = schoolDutyDao.insert(schoolDuty);

        return num;
    }

    @Override
    public List<SchoolDuty> exportDutyList(Map<String, Object> params) {
        String month = (String) params.get("month");
        if(!StringUtils.isEmpty(month)){
            params.put("yearMonth",month.substring(0,month.length()-3));
        }
        return schoolDutyDao.exportDutyList(params);
    }

    @Override
    public int updateDutyUser(Map<String, Object> params) {
        SchoolDuty oldSchoolDuty = new SchoolDuty();
        oldSchoolDuty.setId(Long.valueOf(String.valueOf(params.get("oldId"))));
        oldSchoolDuty.setUserId(Long.valueOf(String.valueOf(params.get("newUserId"))));
        int oldNum = schoolDutyDao.updateById(oldSchoolDuty);

        SchoolDuty newSchoolDuty = new SchoolDuty();
        newSchoolDuty.setId(Long.valueOf(String.valueOf(params.get("newId"))));
        newSchoolDuty.setUserId(Long.valueOf(String.valueOf(params.get("oldUserId"))));
        int newNum = schoolDutyDao.updateById(newSchoolDuty);

        return newNum+oldNum;
    }

    @Override
    public int deleteOtherMonthDuty(Map<String, Object> params) {
        String nowMonth = DateUtil.format(new Date(),"yyyy-MM-dd");
        String beginDate = String.valueOf(params.get("beginDate"));
        String endDate = String.valueOf(params.get("endDate"));
        String dept = String.valueOf(params.get("dept"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer num = 0;
        try {
            Long nowTime = sdf.parse(nowMonth).getTime();
            Long beginDateTime = sdf.parse(beginDate).getTime();
            if(beginDateTime>=nowTime){
                num = schoolDutyDao.deleteOtherMonthDuty(beginDate,endDate,dept);
            }else{
                return -1;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return num;
    }
}
